package com.opal.modules.procurement.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.modules.procurement.entity.PurchaseContract;
import com.opal.modules.procurement.mapper.oracle.PurchaseMapper;
import com.opal.modules.procurement.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public R queryPurchaseContractService(PurchaseContract purchaseContract) {
        if (StringUtils.isBlank(purchaseContract.getMatCode())){
            purchaseContract.setMatCode(null);
        }

        List<PurchaseContract> purchaseContracts = purchaseMapper.queryPurchaseContract(purchaseContract);
        //处理数量下限为两位小数
        for (int i=0;i<purchaseContracts.size();i++){
            try {
                PurchaseContract p= purchaseContracts.get(i);
                String vbdef12 = p.getVbdef12();
                if (vbdef12 !=null && !StringUtils.isBlank(vbdef12) && !"~".equals(vbdef12)){
                    Double d = Double.parseDouble(vbdef12);
                    //转换为只留两位小数
                    p.setVbdef12(String.format("%.2f", d));
                }

            }catch (Exception e){
                log.error("【数量下限】字段转换Double类型失败！");
            }

        }

        if (purchaseContracts ==null || purchaseContracts.size()<=0){
            return new R(WSC.RESTPONSE_OK+1,"没有查询到相关的数据！");
        }

        return new R(WSC.RESTPONSE_OK,null,purchaseContracts);
    }

    @Override
    public R queryTimelyCloseService(String pkOrg) {
        List<Map<String, String>> maps = purchaseMapper.queryTimelyClose(pkOrg);
        if (maps ==null || maps.size()<=0){
            return new R(WSC.RESTPONSE_OK+1,"没有查询到相关的数据！");
        }
        return new R(WSC.RESTPONSE_OK,null,maps);
    }
}
